from flask import Blueprint,jsonify
from models.stat.StatDailySite import StatDailySite

route_chart = Blueprint('chart_page', __name__)

@route_chart.route("/dashboard",methods=["POST"])
def dashboard():
    last_date = StatDailySite.query.order_by(StatDailySite.id.desc()).first().date
    list = StatDailySite.query.filter(StatDailySite.date <= last_date).order_by(StatDailySite.id.desc()).limit(30).all()
    list.reverse()
    resp = {'code': 200, 'msg': '请求成功！', 'data': {}}
    data = {
        "categories": [],
        "series": {
            "orderSum": [],
            "memberSum": []
         }
    }
    if list:
        for item in list:
            data['categories'].append(str(item.date))
            data['series']['memberSum'].append(item.total_member_count)
            data['series']['orderSum'].append(item.total_order_count)

    resp['data'] = data
    return jsonify(resp)

@route_chart.route("/finance",methods=["POST"])
def finance():
    last_date = StatDailySite.query.order_by(StatDailySite.id.desc()).first().date
    list = StatDailySite.query.filter(StatDailySite.date <= last_date).order_by(StatDailySite.id.desc()).limit(30).all()
    list.reverse()

    resp = {'code': 200, 'msg': '请求成功!', 'data': {}}
    data = {
        "categories": [],
        "series": {
            "finance": []
        }
    }

    if list:
        for item in list:
            data['categories'].append(str(item.date))
            data['series']['finance'].append(float(item.total_pay_money))

    resp['data'] = data
    return jsonify(resp)

@route_chart.route("/share",methods=["POST"])
def share():
    last_date = StatDailySite.query.order_by(StatDailySite.id.desc()).first().date
    list = StatDailySite.query.filter(StatDailySite.date <= last_date).order_by(StatDailySite.id.desc()).limit(30).all()
    list.reverse()

    resp = {'code': 200, 'msg': '请求成功!', 'data': {}}
    data = {
        "categories": [],
        "series": {
            "share": []
        }
    }

    if list:
        for item in list:
            data['categories'].append(str(item.date))
            data['series']['share'].append(float(item.total_shared_count))

    resp['data'] = data
    return jsonify(resp)
